package makovo.ajax;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import makovo.data.Resources;
import makovo.entities.Applicant.ApplicantStep;

import org.apache.struts2.interceptor.SessionAware;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.Gson;

public class EditProfile extends HttpServlet implements SessionAware {
	private static final long serialVersionUID = 1L;
	private String mCurp = null;

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		mCurp = (String) session.getAttribute("User.idSender");
		String method = (String) request.getParameter("m");
		boolean res = false;

		if (method.equals("bi"))
			res = editBasicInfo(request);
		else if (method.equals("si"))
			res = editSocInfo(request);
		else if (method.equals("pi"))
			res = editProjInfo(request);

		Gson gson = new Gson();
		response.getWriter().write(gson.toJson(res));
	}

	private boolean editBasicInfo(HttpServletRequest request) {
		String name = request.getParameter("first-name").toUpperCase();
		String last = request.getParameter("last-name").toUpperCase();
		String secondLast = request.getParameter("second-last-name").toUpperCase();
		String birth = request.getParameter("bitrh");
		int typeID = Integer.valueOf(request.getParameter("type-id"));
		String seqId = request.getParameter("fol-id").toUpperCase();
		int civ = Integer.valueOf(request.getParameter("civ"));
		String phone = request.getParameter("phone");

		String state = request.getParameter("state").toUpperCase();
		int stateId = Integer.valueOf(request.getParameter("state-id"));
		String city = request.getParameter("city").toUpperCase();
		int cityId = Integer.valueOf(request.getParameter("city-id"));
		String loc = request.getParameter("loc").toUpperCase();
		int locId = Integer.valueOf(request.getParameter("loc-id"));
		String cp = request.getParameter("cp");
		String street = request.getParameter("street").toUpperCase();
		String extNumber = request.getParameter("ext-num").toUpperCase();
		String intNumber = request.getParameter("int-num").toUpperCase();
		float lonng = Float.valueOf(request.getParameter("lonng"));
		float lat = Float.valueOf(request.getParameter("lat"));

		try {
			int addressID = Resources.saveAddress(state, stateId, city, cityId, loc, locId, cp, street, extNumber, intNumber,
					Double.valueOf(lonng), Double.valueOf(lat),"");

			if (addressID == -1)
				return false;
			Resources.modifyApplicantInformation(mCurp, name, last, secondLast, birth, typeID, seqId, civ, phone, addressID);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return true;
	}

	private boolean editSocInfo(HttpServletRequest request) {
		int ant = Integer.valueOf(request.getParameter("ant"));
		int dependants = Integer.valueOf(request.getParameter("dependants"));
		int jobTypeId = Integer.valueOf(request.getParameter("job-type-id"));
		String ent = request.getParameter("ent").toUpperCase();
		double earn = Double.valueOf(request.getParameter("earn"));
		String job = request.getParameter("job").toUpperCase();

		String state = request.getParameter("state").toUpperCase();
		int stateId = Integer.valueOf(request.getParameter("state-id"));
		String city = request.getParameter("city").toUpperCase();
		int cityId = Integer.valueOf(request.getParameter("city-id"));
		String loc = request.getParameter("loc").toUpperCase();
		int locId = Integer.valueOf(request.getParameter("loc-id"));
		String cp = request.getParameter("cp");
		String street = request.getParameter("street").toUpperCase();
		String extNumber = request.getParameter("ext-num").toUpperCase();
		String intNumber = request.getParameter("int-num").toUpperCase();
		float lonng = Float.valueOf(request.getParameter("lonng"));
		float lat = Float.valueOf(request.getParameter("lat"));

		try {
			int addressId = Resources.saveAddress(state, stateId, city, cityId, loc, locId, cp, street, extNumber, intNumber,
					Double.valueOf(lonng), Double.valueOf(lat),"");

			if (addressId == -1)
				return false;

			return Resources.saveApplicantSocInfo(mCurp, addressId, ant, dependants, jobTypeId, ent, earn, job);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return true;
	}

	private boolean editProjInfo(HttpServletRequest request) {
		boolean res = false;
		String projId = request.getParameter("proj-id").toUpperCase();
		int projTypeId = Integer.valueOf(request.getParameter("proj-type-id"));
		int projSettType = Integer.valueOf(request.getParameter("proj-sett-type"));

		String state = request.getParameter("state").toUpperCase();
		int stateId = Integer.valueOf(request.getParameter("state-id"));
		String city = request.getParameter("city").toUpperCase();
		int cityId = Integer.valueOf(request.getParameter("city-id"));
		String loc = request.getParameter("loc").toUpperCase();
		int locId = Integer.valueOf(request.getParameter("loc-id"));
		String cp = request.getParameter("cp");
		String street = request.getParameter("street").toUpperCase();
		String extNumber = request.getParameter("ext-num").toUpperCase();
		String intNumber = request.getParameter("int-num").toUpperCase();
		float lonng = Float.valueOf(request.getParameter("lonng"));
		float lat = Float.valueOf(request.getParameter("lat"));

		String refOne = request.getParameter("ref-one").toUpperCase();
		String refTwo = request.getParameter("ref-two").toUpperCase();
		int vialType = Integer.valueOf(request.getParameter("vial-type"));

		try {
			int addressId = Resources.saveAddress(state, stateId, city, cityId, loc, locId, cp, street, extNumber, intNumber,
					Double.valueOf(lonng), Double.valueOf(lat),"");

			if (addressId == -1)
				return false;

			res = Resources.saveReferences(addressId, refOne, refTwo, vialType);

			if (!res)
				return false;

			return Resources.saveApplicantProject(mCurp, projId, projTypeId, projSettType, addressId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return true;
	}

	@Override
	public void setSession(Map<String, Object> session) {
	}
}
